package com.lagou.task01;

import java.lang.reflect.Array;
import java.util.Random;

/**
 *
 *  定义一个长度为[16][16]的整型二维数组并输入或指定所有位置的元素值，分别实现二维数组中所有行和列的累加和并打印
 *  再分别实现对角线上所有元素累加和并打印
 *
 * */
public class DoubleDimensionalArray {

    public int sourceArr[][];

    public void setSourceArr(){

        this.sourceArr = new int[16][16];

        Random ra = new Random();

        //对数组复制 加入随机数
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                int randomNum = ra.nextInt(100);    //随机数 范围 0-99
                sourceArr[i][j] = randomNum;
                System.out.print(sourceArr[i][j] + "\t");
            }
            System.out.println();
        }
        System.out.println("数组赋值完毕");
    }

    //每行累加和
    public void addLine(){
        //每行的累加和
        for (int i = 0; i < 16; i++) {
            System.out.println("开始计算第" + i + "行的累加和：");
            int sum = 0;
            String showStr = "";
            for (int j = 0; j < 16; j++) {
                int targetNum = sourceArr[i][j];
                sum += targetNum;
                if (j == 15){
                    showStr = showStr + targetNum + " = " + sum;
                }else{
                    showStr = showStr + targetNum + " + ";
                }
            }
            System.out.println("加法运算式：" + showStr);
            System.out.println("第" + i + "行的累加和结果是：" + sum);
            System.out.println("----------------------------------");
            System.out.println();
        }
    }

    //每列累加和
    public void addList(){
        //每列的累加和
        for (int i = 0; i < 16; i++) {
            System.out.println("开始计算第" + i + "列的累加和：");
            int sum = 0;
            String showStr = "";
            for (int j = 0; j < 16; j++) {
                int targetNum = sourceArr[j][i];
                sum += targetNum;
                if (j == 15){
                    showStr = showStr + targetNum + " = " + sum;
                }else{
                    showStr = showStr + targetNum + " + ";
                }
            }
            System.out.println("加法运算式：" + showStr);
            System.out.println("第" + i + "列的累加和结果是：" + sum);
            System.out.println("----------------------------------");
            System.out.println();
        }
    }

    //左上到右下对角线
    public void addDiagonal1(){
        //对角线累加和
        int sum = 0;
        String showStr = "";
        for (int i = 0; i < 16; i++) {
            int targetNum = sourceArr[i][i];
            sum += sourceArr[i][i];
            if (i == 15){
                showStr = showStr + targetNum + " = " + sum;
            }else{
                showStr = showStr + targetNum + " + ";
            }
        }
        System.out.println("加法运算式：" + showStr);
        System.out.println("左上到右下对角线的累加和结果是：" + sum);
        System.out.println("----------------------------------");
        System.out.println();
    }

    //右上到左下对角线
    public void addDiagonal2() {
        //对角线累加和
        int sum = 0;
        String showStr = "";
        for (int i = 0; i < 16; i++) {
            int targetNum = sourceArr[i][15-i];
            sum += targetNum;
            if (i == 15){
                showStr = showStr + targetNum + " = " + sum;
            }else{
                showStr = showStr + targetNum + " + ";
            }
        }
        System.out.println("加法运算式：" + showStr);
        System.out.println("右上到左下对角线的累加和结果是：" + sum);
        System.out.println("----------------------------------");
        System.out.println();

    }
}
